package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CheckDto;
import com.itheima.common.ChoiceDto;
import com.itheima.common.PageResult;
import com.itheima.mapper.*;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.hm.HmCompanys;
import com.itheima.pojo.hm.HmQuestions;
import com.itheima.pojo.hm.HmSubjects;
import com.itheima.pojo.hm.HmSubjectsDirectorys;
import com.itheima.pojo.pe.QuestionsOptions;
import com.itheima.service.HmSubjectsService;
import com.itheima.service.QuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class QuestionsServiceImpl implements QuestionsService {
    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private HmSubjectsMapper hmSubjectsMapper;
    @Autowired
    private DirectorysMapper hmSubjectsDirectorysMapper;
    @Autowired
    private CompanysMapper hmCompanysMapper;
    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private QuestionsOptionsMapper questionsOptionsMapper;

    @Override
    public PageResult findList(Long page, Long pagesize, Integer subjectID, String question) {
        IPage<HmQuestions> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<HmQuestions> wrapper = new LambdaQueryWrapper<>();
        if (subjectID != null) {
            wrapper.eq(HmQuestions::getSubjectID, subjectID);
        }
        if (question != null) {
            wrapper.like(HmQuestions::getQuestion, question);
        }
        IPage<HmQuestions> hmQuestionsIPage = questionsMapper.selectPage(p, wrapper);
        //从其他表拿到数据补全
        List<HmQuestions> questions = hmQuestionsIPage.getRecords();
        for (HmQuestions hmQuestion : questions) {
            //拿到学科名称
            HmSubjects hmSubject = hmSubjectsMapper.selectById(hmQuestion.getSubjectID());
            hmQuestion.setSubject(hmSubject.getSubjectName());
            //拿到目录名称
            HmSubjectsDirectorys hmSubjectsDirectory = hmSubjectsDirectorysMapper.selectById(hmQuestion.getCatalogID());
            hmQuestion.setCatalog(hmSubjectsDirectory.getDirectoryName());
            //拿到企业名称
            System.out.println("11111111111111111111111111111+"+hmQuestion.getEnterpriseID());
            HmCompanys hmCompany = hmCompanysMapper.selectById(hmQuestion.getEnterpriseID());
            hmQuestion.setEnterprise(hmCompany.getShortName());
            //拿到录入人
            BsUser bsUser = userMapper.selectById(hmQuestion.getCreatorID());
            hmQuestion.setCreator(bsUser.getUsername());
        }
        PageResult result = new PageResult();
        result.setCounts(hmQuestionsIPage.getTotal());
        result.setItems(questions);
        result.setPage(page);
        result.setPageSize(pagesize);
        result.setPages(hmQuestionsIPage.getPages());
        return result;
    }

    @Override
    public void add(HmQuestions hmQuestion) {
        //审核发布后要修改数据,这里先给死
        hmQuestion.setChkState(0);
        //利用session得到登录id放入CreatorID
        hmQuestion.setCreatorID(2);
        hmQuestion.setAddDate(LocalDateTime.now());
        hmQuestion.setNumber(UUID.randomUUID().toString());
        hmQuestion.setPublishState(0);
        hmQuestion.setPublishDate(LocalDateTime.now());

        questionsMapper.insert(hmQuestion);
        //拿到添加后的题目id
        Integer id = hmQuestion.getId();
        //将选项数据放入表
        List<QuestionsOptions> item = hmQuestion.getOptions();
        for (QuestionsOptions o : item) {
            o.setQuestionsID(id);
            questionsOptionsMapper.insert(o);
        }
    }

    @Override
    public void removeById(Integer id) {
        //先删除题目对应的选项
        LambdaQueryWrapper<QuestionsOptions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsOptions::getQuestionsID, id);
        questionsOptionsMapper.delete(wrapper);
        //删除题库里的题目
        questionsMapper.deleteById(id);
    }

    @Override
    public HmQuestions findQuestion(Integer id) {
        HmQuestions hmQuestion = questionsMapper.selectById(id);
        LambdaQueryWrapper<QuestionsOptions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsOptions::getQuestionsID, id);
        List<QuestionsOptions> questionsOptions = questionsOptionsMapper.selectList(wrapper);
        hmQuestion.setOptions(questionsOptions);
        //拿到学科名称
        HmSubjects hmSubject = hmSubjectsMapper.selectById(hmQuestion.getSubjectID());
        hmQuestion.setSubject(hmSubject.getSubjectName());
        //拿到目录名称
        HmSubjectsDirectorys hmSubjectsDirectory = hmSubjectsDirectorysMapper.selectById(hmQuestion.getCatalogID());
        hmQuestion.setCatalog(hmSubjectsDirectory.getDirectoryName());
        return hmQuestion;
    }

    @Override
    public void modifyById(HmQuestions hmQuestion) {
        questionsMapper.updateById(hmQuestion);
        //删除选项表里的数据再添加新的
        LambdaQueryWrapper<QuestionsOptions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsOptions::getQuestionsID, hmQuestion.getId());
        questionsOptionsMapper.delete(wrapper);
        //添加
        List<QuestionsOptions> item = hmQuestion.getOptions();
        for (QuestionsOptions o : item) {
            o.setQuestionsID(hmQuestion.getId());
            questionsOptionsMapper.insert(o);
        }
    }

    @Override
    public void modifyChoiceState(ChoiceDto choiceDto) {
        LambdaQueryWrapper<HmQuestions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HmQuestions::getId, choiceDto.getId());
        HmQuestions hmQuestion = questionsMapper.selectOne(wrapper);
        hmQuestion.setIsChoice(choiceDto.getChoiceState());
        questionsMapper.updateById(hmQuestion);
    }

    @Override
    public PageResult choice(Long page, Long pagesize) {
        IPage<HmQuestions> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<HmQuestions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HmQuestions::getIsChoice, 1);
        IPage<HmQuestions> hmQuestionsIPage = questionsMapper.selectPage(p, wrapper);
        List<HmQuestions> questions = hmQuestionsIPage.getRecords();
        for (HmQuestions hmQuestion : questions) {
            //拿到学科名称
            HmSubjects hmSubject = hmSubjectsMapper.selectById(hmQuestion.getSubjectID());
            hmQuestion.setSubject(hmSubject.getSubjectName());
            //拿到目录名称
            HmSubjectsDirectorys hmSubjectsDirectory = hmSubjectsDirectorysMapper.selectById(hmQuestion.getCatalogID());
            hmQuestion.setCatalog(hmSubjectsDirectory.getDirectoryName());
            //拿到企业名称
            HmCompanys hmCompany = hmCompanysMapper.selectById(hmQuestion.getEnterpriseID());
            hmQuestion.setEnterprise(hmCompany.getShortName());
            //拿到录入人
            BsUser bsUser = userMapper.selectById(hmQuestion.getCreatorID());
            hmQuestion.setCreator(bsUser.getUsername());
        }
        PageResult result = new PageResult();
        result.setCounts(hmQuestionsIPage.getTotal());
        result.setItems(questions);
        result.setPage(page);
        result.setPageSize(pagesize);
        result.setPages(hmQuestionsIPage.getPages());
        return result;
    }

    @Override
    public void checkQuestion(CheckDto checkDto) {
        HmQuestions hmQuestion = questionsMapper.selectById(checkDto.getId());
        hmQuestion.setChkState(checkDto.getChkState());
        hmQuestion.setChkDate(LocalDateTime.now());
        hmQuestion.setChkRemarks(checkDto.getChkRemarks());
        hmQuestion.setCreatorID(2);
        questionsMapper.updateById(hmQuestion);
    }

    @Override
    public void modifyPublishState(Integer id, Integer publishState) {
        HmQuestions hmQuestion = questionsMapper.selectById(id);
        hmQuestion.setPublishState(publishState);
        hmQuestion.setPublishDate(LocalDateTime.now());
        questionsMapper.updateById(hmQuestion);
    }
}
